/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.basic.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import me.zhengjie.modules.basic.domain.BasicAc01;
import me.zhengjie.modules.basic.domain.BasicAe03;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.util.ValidateUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.basic.service.BasicAe03Service;
import me.zhengjie.modules.basic.domain.dto.BasicAe03QueryCriteria;
import me.zhengjie.modules.basic.mapper.BasicAe03Mapper;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.util.*;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import me.zhengjie.utils.PageResult;

/**
 * @author ly
 * @description 服务实现
 * @date 2025-04-30
 **/
@Service
@RequiredArgsConstructor
public class BasicAe03ServiceImpl extends ServiceImpl<BasicAe03Mapper, BasicAe03> implements BasicAe03Service {

    private final BasicAe03Mapper basicAe03Mapper;
    @Resource
    BusinessLogService businessLogService;
    @Resource
    private final SequenceService sequenceService;


    @Override
    public PageResult<BasicAe03> queryAll(BasicAe03QueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(basicAe03Mapper.findAll(criteria, page));
    }

    @Override
    public List<BasicAe03> queryAll(BasicAe03QueryCriteria criteria) {
        return basicAe03Mapper.findAll(criteria);
    }

    @Override
    public List<BasicAe03> getIa20i2(BasicAe03 map){
        return basicAe03Mapper.getIa20i2(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(BasicAe03 resources) throws IOException {

        // 校验
        check(resources);
        // 日志
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> param = objectMapper.convertValue(resources, Map.class);
        param.put("aaz002", sequenceService.nextSequence(new Sequence("aaz002")));
        param.put("aaa121", YHConstants.AAA121_TFYHXXXZ);
        param.put("aae016", Constant.AAE016_1_SHTG);
        param.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        param.put("aac001", resources.getAaz010());
        param.put("aac003", resources.getAae734());
        BusinessLog businessLog = BusinessLog.singleLog(param);
        businessLogService.save(businessLog);
        // 保存
        resources.setAae011(SecurityUtils.getCurrentUsername());
        resources.setAae036(DateUtil.getStringCurDateTime());
//        resources.setAab004(null);
        basicAe03Mapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BasicAe03 resources) throws IOException {
//        resources.setAab004(null);
//        BasicAe03 basicAe03 = getById(resources.getAaz003());
        // 日志
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> param = objectMapper.convertValue(resources, Map.class);
        param.put("aaz002", sequenceService.nextSequence(new Sequence("aaz002")));
        param.put("aaa121", YHConstants.AAA121_TFYHXXBG);
        param.put("aae016", Constant.AAE016_1_SHTG);
        param.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        param.put("aac001", resources.getAaz010());
        param.put("aac003", resources.getAae734());
        BusinessLog businessLog = BusinessLog.singleLog(param);
        businessLogService.save(businessLog);

        resources.copy(resources);
        basicAe03Mapper.updateById(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) throws IOException {
        // 日志
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> param = new HashMap<>();
        param.put("aaz002", sequenceService.nextSequence(new Sequence("aaz002")));
        param.put("aaa121", YHConstants.AAA121_TFYHXXSC);
        param.put("aab001", ids.get(0));
        param.put("aae016", Constant.AAE016_1_SHTG);
        param.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        BusinessLog businessLog = BusinessLog.singleLog(param);
        businessLogService.save(businessLog);
        basicAe03Mapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<BasicAe03> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BasicAe03 basicAe03 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("当事人编号", basicAe03.getAaz010());
            map.put("当事人类别", basicAe03.getAaa028());
            map.put("银行行别代码", basicAe03.getAaf200());
            map.put("地方银行行别代码", basicAe03.getAaf216());
            map.put("地方银行名称", basicAe03.getAaf217());
            map.put("银行编号", basicAe03.getAae008());
            map.put("人行支付系统行号", basicAe03.getAaf002());
            map.put("银行户名", basicAe03.getAae009());
            map.put("银行账号", basicAe03.getAae010());
            map.put("开户行名称", basicAe03.getAae668());
            map.put("当事人银行账号用途类别", basicAe03.getAaa082());
            map.put("险种类型", basicAe03.getAae140());
            map.put("开始年月", basicAe03.getAae041());
            map.put("终止年月", basicAe03.getAae042());
            map.put("有效标志", basicAe03.getAae100());
            map.put("备注", basicAe03.getAae013());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 前置校验
     */
    public void check(BasicAe03 basicAe03) {
        // 非空校验
        ValidateUtil.isEmptyThrowException(basicAe03.getAaz010(),"当事人编号");
        ValidateUtil.isEmptyThrowException(basicAe03.getAae140(),"险种");

        //重复校验
        Map map = new HashMap();
        map.put("aaz010",basicAe03.getAaz010());
        map.put("aae140",basicAe03.getAae140());
        List<BasicAe03> ae03s = basicAe03Mapper.selectByMap(map);
        if (!ValidateUtil.isEmpty(ae03s)) {
            throw new RuntimeException("【" + basicAe03.getAaz010() + basicAe03.getAae140() + "】人员基础信息已存在，直接修改即可");
        }
    }
}
